//
//  OtherViewModel.swift
//  EarbudsFunctions
//
//  Created by 李然 on 2024/5/16.
//

import Foundation
import RxDataSources

public enum OtherSectionType {
    case weather
}

public struct OtherSection {
    public let type: OtherSectionType
    public var items: [OtherSectionItem]
}

extension OtherSection: SectionModelType {
    public init(original: OtherSection, items: [OtherSectionItem]) {
        self = original
        self.items = items
    }
}

public enum OtherSectionItem {
    // 每小时更新
    case weatherItem(cellModel: DefaultSwitchCellViewModel)
    // 天气格式
    case temperatureFormatItem(cellModel: DefaultCellViewModel)
    // 定位
    case locationItem(cellModel: DefaultCellViewModel)
}

public class OtherViewModel: ViewModel {
    
    public let dataSource: BehaviorRelay<[OtherSection]>
    
    public override init() {
        
        var section = [OtherSection]()
        
        do {
            let weatherCellViewModel = DefaultSwitchCellViewModel(isEnabel: Weather.shared.updateEveryHour.value)
            weatherCellViewModel.switchChanged
                .skip(1)
                .bind(to: Weather.shared.updateEveryHour)
                .disposed(by: weatherCellViewModel.rx.disposeBag)
            
            let unitCellViewModel = DefaultCellViewModel()
            unitCellViewModel.arrow.accept(true)
            
            let cityCellViewModel = DefaultCellViewModel()
            cityCellViewModel.arrow.accept(true)
            cityCellViewModel.detail.accept(Weather.shared.cityName.value)
            Weather.shared.cityName.bind(to: cityCellViewModel.detail).disposed(by: cityCellViewModel.rx.disposeBag)
            
            let items = [OtherSectionItem.weatherItem(cellModel: weatherCellViewModel),
                         OtherSectionItem.temperatureFormatItem(cellModel: unitCellViewModel),
                         OtherSectionItem.locationItem(cellModel: cityCellViewModel)]
            
            section.append(OtherSection(type: .weather, items: items))
        }
        
        self.dataSource = BehaviorRelay(value: section)
        
        super.init()
    }
}
